package com.xml.dom.ctl;

import static org.junit.Assert.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.Before;
import org.junit.Test;

public class BookCtlImpTest {

	BookCtl ctl;
	private static Map<String, String> ADD_BOOK;
	private static Map<String, String> EDT_BOOK;
	
	static {
		ADD_BOOK = new HashMap<String, String>();
		ADD_BOOK.put("author", "test");
		ADD_BOOK.put("name", "testName");
		ADD_BOOK.put("price", "testprice");
		EDT_BOOK = new HashMap<String, String>();
		EDT_BOOK.put("author", "edit");
		EDT_BOOK.put("name", "edtName");
		EDT_BOOK.put("price", "edtprice");
	}

	@Before
	public void setUp() throws Exception {
		ctl = BookCtlImp.getInterface();
	}
	
	@Test
	public void testGetInterface() {
		if(null == ctl || !(ctl instanceof BookCtl)) {
			fail();
		}
	}

	@Test
	public void testGetAllBooks() {
		List<Map<String, String>> books = ctl.getAllBooks();
		assertEquals(3, books.size());
	}

	@Test
	public void testGetBooksByTag() {
		List<Map<String, String>> books = ctl.getBooksByTag("name", "Effective Java");
		assertEquals(1, books.size());
	}

	@Test
	public void testGetBookDataByID() {
		Map<String,String> book = ctl.getBookDataByID("0");
		assertEquals("Joshua Bloch", book.get("author"));
		assertEquals("38.36", book.get("price"));
		assertEquals("Effective Java", book.get("name"));
	}

	@Test
	public void testAdd() {
		ctl.add(ADD_BOOK);
		
		assertEquals(4, ctl.getAllBooks().size());

		Map<String,String> book = ctl.getBookDataByID("3");
		assertEquals(ADD_BOOK.get("author"), book.get("author"));
		assertEquals(ADD_BOOK.get("price"), book.get("price"));
		assertEquals(ADD_BOOK.get("name"), book.get("name"));
	}

	@Test
	public void testEdit() {
		ctl.edit("3", EDT_BOOK);
		assertEquals(4, ctl.getAllBooks().size());

		Map<String,String> book = ctl.getBookDataByID("3");
		assertEquals(EDT_BOOK.get("author"), book.get("author"));
		assertEquals(EDT_BOOK.get("price"), book.get("price"));
		assertEquals(EDT_BOOK.get("name"), book.get("name"));
}

	@Test
	public void testDelete() {
		ctl.delete("3");
		assertEquals(3, ctl.getAllBooks().size());
	}

}
